package cn.hbads.test.context;

import cn.hbads.test.task.ExecuteTask;
import cn.hutool.core.collection.CollUtil;
import lombok.Getter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <b> 任务执行上下文持有容器 </b>
 *
 * @author : yvvb
 * @date : 2025/5/11
 */
@Getter
public class TaskContextManager {
    private TaskContext taskContext;
    private Map<String, Object> properties = new HashMap<>();

    public void initContext(String batchNo, List<ExecuteTask> tasks) {
        final var taskMap = tasks.stream()
                .collect(Collectors.toMap(ExecuteTask::getTaskName, Function.identity()));
        this.taskContext = new TaskContext(batchNo, taskMap);
        if (CollUtil.isNotEmpty(this.properties)) {
            this.taskContext.putAll(this.properties);
        }
    }

    public void prepareProperties(Map<String, Object> properties) {
        this.properties.putAll(properties);
    }


}
